//
//  ST_BTPlayCoverVideoController.swift
//  BTVideo
//
//  Created by DouLei on 2019/8/6.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit


class ST_BTPlayCoverVideoController: ST_BTLightBarViewController {

    
    var user:ST_BTUserinfo!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = Color.black
        
        left_nav_btn.setImage(ST_ImageUtils.imageName("返回_white"), for: UIControl.State.normal)
        
        own_navigation_bar.backgroundColor = Color.clear
        
        view.insertSubview(cover_image_view, belowSubview: own_navigation_bar)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        cover_image_view.pin_setImage(from: user.cover_video_img.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        play()
    }
    
    lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.backgroundColor = Color.black
        return control
    }()
    
    
    //    MARK: - Play Video
    var player:AVPlayer?
    
    
    var playerLayer:AVPlayerLayer?
    
    // 0 :播放中，1:暂停
    private var playState = 0
    
    func stop(){
        
        self.playerLayer?.removeFromSuperlayer()
        self.player?.pause()
        self.player = nil
        playState = 1
    }
    
    deinit {
        
        self.stop()
        //        print("停止了播放视频")
    }
    
    func play(){
        
        guard  let playUrl = URL.init(string: user.cover_video)  else {
            return
        }
        
        
        
        let palyerItem = AVPlayerItem(url: playUrl)
        //创建ACplayer：负责视频播放
        self.player = AVPlayer.init(playerItem: palyerItem)
        
        self.player?.rate = 1.0//播放速度 播放前设置
        //创建显示视频的图层
        let playerLayer = AVPlayerLayer.init(player: player)
        playerLayer.videoGravity = .resizeAspectFill
        playerLayer.frame = self.view.bounds
        self.playerLayer = playerLayer
        
        self.view.layer.insertSublayer(playerLayer, above: self.cover_image_view.layer)
        
        guard let player = self.player else { return  }
        
        player.play()
        
        NotificationCenter.default.addObserver(self, selector: #selector(self.player_did_finish_playing(note:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: palyerItem)
    }
    
    @objc func player_did_finish_playing(note: Notification) {
        
        //        print("播放完成")
        
        if let playerItem = note.object as? AVPlayerItem{
            
            if let currentItem = self.player?.currentItem,playerItem == currentItem{
                
                playState = 1
                if let player = self.player{
                    player.seek(to: CMTime.zero)
                    player.play()
                    playState = 0
                }
                
                return
            }
        }
        
        
    }
    
}
